package com.from.menlo.park; 

import java.io.IOException;
import java.util.Iterator;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class LocationEBrain extends Activity {

	public LocationManager locationManager;	
	Button start, stop;
	TextView address, laty;
	ClickListener listener;
	MyLocationListener locationListener;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.location);

		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		start = (Button)findViewById(R.id.start);
		stop = (Button)findViewById(R.id.stop);

		address = (TextView)findViewById(R.id.address);
		laty = (TextView)findViewById(R.id.laty);

		listener = new ClickListener();
		locationListener = new MyLocationListener();

		start.setOnClickListener(listener);
		stop.setOnClickListener(listener);		

	}


	public class ClickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			if(v.getId() == R.id.start){
				//Start button clicked
				startGps(); //Start the GPS

			}

			else if(v.getId() == R.id.stop){
				//Stop button clicked
				stopGps(); //Stop the GPS
			}			
		}    	
	}

	public class MyLocationListener implements LocationListener{

		@Override
		public void onLocationChanged(Location location) {
			// TODO Auto-generated method stub
			Double lat = location.getLatitude();
			Double longi = location.getLongitude();
			laty.setText("Latitude = "+lat+"\nLongitude = "+longi);
			getAddress(location);
		}

		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub

		}

	}


	public void startGps(){

		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.NO_REQUIREMENT);
		criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);

		String provider = locationManager.getBestProvider(criteria, true); 
		Location location = locationManager.getLastKnownLocation(provider);
		locationListener.onLocationChanged(location);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, locationListener);

	}


	public void stopGps(){
		locationManager.removeUpdates(locationListener);
	}

	public void getAddress(Location location){

		Geocoder  geocoder = new Geocoder(LocationEBrain.this);

		try {

			final Iterator<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1).iterator();

			runOnUiThread(new Runnable(){

				@Override
				public void run() {
					if (addresses != null) {
						while (addresses.hasNext()) {
							Address namedLoc = addresses.next();
							//String placeName = namedLoc.getLocality();
							String featureName = namedLoc.getFeatureName();
							//String country = namedLoc.getCountryName();
							//String road = namedLoc.getThoroughfare();
							address.setText(namedLoc+featureName);
						}
					}

				}

			});


		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onPause(){
		stopGps();
		super.onPause();
	}
}